
package Vista;

import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Bodega extends javax.swing.JFrame {
    private Controlador.Bodega bodegaController;
    private TableRowSorter trsfiltro;

    /**
     * Creates new form Bodega
     */
    public Bodega() {
        initComponents();
        setResizable(true);
        setLocationRelativeTo(null);
        bodegaController = new Controlador.Bodega();
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableProductos = new javax.swing.JTable();
        jPanelProducto = new javax.swing.JPanel();
        jLabelNombre = new javax.swing.JLabel();
        jLabelPlataforma = new javax.swing.JLabel();
        jLabelCantidad = new javax.swing.JLabel();
        jLabelValor = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jTextFieldCantidad = new javax.swing.JTextField();
        jTextFieldPrecio = new javax.swing.JTextField();
        jComboBoxPlataforma = new javax.swing.JComboBox();
        jButtonIngresa = new javax.swing.JButton();
        jLabelCategoria = new javax.swing.JLabel();
        jComboBoxCategoria = new javax.swing.JComboBox();
        jTextFieldID = new javax.swing.JTextField();
        jLabelID = new javax.swing.JLabel();
        jButtonLimpiar = new javax.swing.JButton();
        jLabelBuscar = new javax.swing.JLabel();
        jTextFieldBuscar = new javax.swing.JTextField();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Bodega"));

        jTableProductos.setModel(this.getCustomTableModel());
        jTableProductos.setName("");
        jTableProductos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableProductos.getTableHeader().setReorderingAllowed(false);
        jTableProductos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableProductosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableProductos);

        jPanelProducto.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Producto", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));

        jLabelNombre.setText("Nombre");

        jLabelPlataforma.setText("Plataforma");

        jLabelCantidad.setText("Cantidad");

        jLabelValor.setText("Valor Unidad");

        jComboBoxPlataforma.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"","PC", "PS3", "XBOX", "WII" }));
        jComboBoxPlataforma.setSelectedItem("");
        jComboBoxPlataforma.setToolTipText("");

        jButtonIngresa.setText("Guardar");
        jButtonIngresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jLabelCategoria.setText("Categoria");

        jComboBoxCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "","Aventura", "Shooter", "Accion", "Estrategia", "Lucha", "Conduccion", "Ajedrez", "Deportes", "MMORPG", "Arcade" }));
        jComboBoxCategoria.setSelectedItem("");

        jTextFieldID.setEditable(false);

        jLabelID.setText("ID");

        jButtonLimpiar.setText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelProductoLayout = new javax.swing.GroupLayout(jPanelProducto);
        jPanelProducto.setLayout(jPanelProductoLayout);
        jPanelProductoLayout.setHorizontalGroup(
            jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelProductoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelProductoLayout.createSequentialGroup()
                        .addComponent(jButtonLimpiar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                        .addComponent(jButtonIngresa))
                    .addGroup(jPanelProductoLayout.createSequentialGroup()
                        .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelNombre)
                            .addComponent(jLabelID))
                        .addGap(41, 41, 41)
                        .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldNombre)
                            .addComponent(jTextFieldID)))
                    .addGroup(jPanelProductoLayout.createSequentialGroup()
                        .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelCantidad)
                            .addComponent(jLabelValor)
                            .addComponent(jLabelCategoria)
                            .addComponent(jLabelPlataforma))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxPlataforma, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBoxCategoria, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextFieldPrecio)
                            .addComponent(jTextFieldCantidad))))
                .addContainerGap())
        );
        jPanelProductoLayout.setVerticalGroup(
            jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelProductoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNombre)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPlataforma)
                    .addComponent(jComboBoxPlataforma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCategoria)
                    .addComponent(jComboBoxCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad)
                    .addComponent(jTextFieldCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelValor)
                    .addComponent(jTextFieldPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonIngresa)
                    .addComponent(jButtonLimpiar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabelBuscar.setText("Buscar");

        jTextFieldBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextFieldBuscarKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabelBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelBuscar)
                    .addComponent(jTextFieldBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 1012, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 496, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public String getjComboBoxCategoria() {
        return jComboBoxCategoria.getSelectedItem().toString();
    }
    public String getjComboBoxCategoriaINDEX() {
        return String.valueOf(jComboBoxCategoria.getSelectedIndex());
    }
    public String getjComboBoxPlataforma() {
        return jComboBoxPlataforma.getSelectedItem().toString();
    }
    public String getjComboBoxPlataformaINDEX() {
        return String.valueOf(jComboBoxPlataforma.getSelectedIndex());
    }
    public String getjTextFieldCantidad() {
        return jTextFieldCantidad.getText();
    }
    public String getjTextFieldNombre() {
        return jTextFieldNombre.getText();
    }
    public String getjTextFieldID() {
        return jTextFieldID.getText();
    }
    public String getjTextFieldPrecio() {
        return jTextFieldPrecio.getText();
    }
    public void setjComboBoxCategoria(String categoria) {
        this.jComboBoxCategoria.setSelectedItem(categoria);
    }
    public void setjComboBoxPlataforma(String plataforma) {
        this.jComboBoxPlataforma.setSelectedItem(plataforma);
    }
    public void setjTextFieldCantidad(String cantidad) {
        this.jTextFieldCantidad.setText(cantidad);
    }
    public void setjTextFieldID(String id) {
        this.jTextFieldID.setText(id);
    }
    public void setjTextFieldNombre(String nombre) {
        this.jTextFieldNombre.setText(nombre);
    }
    public void setjTextFieldPrecio(String precio) {
        this.jTextFieldPrecio.setText(precio);
    }
    public String getjTextFieldBuscar() {
        return jTextFieldBuscar.getText();
    }
    public void setjTextFieldBuscar(String buscar) {
        this.jTextFieldBuscar.setText(buscar);
    }
    
    /**
     * Evento para llenado del formulario
     * @param evt 
     */
    private void jTableProductosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableProductosMouseClicked
        setjTextFieldID(jTableProductos.getValueAt(this.jTableProductos.getSelectedRow(),0).toString());
        setjTextFieldNombre(jTableProductos.getValueAt(this.jTableProductos.getSelectedRow(),1).toString());
	setjTextFieldCantidad(jTableProductos.getValueAt(this.jTableProductos.getSelectedRow(),4).toString());      
        setjTextFieldPrecio(jTableProductos.getValueAt(this.jTableProductos.getSelectedRow(),5).toString());
        setjComboBoxPlataforma(jTableProductos.getValueAt(this.jTableProductos.getSelectedRow(),2).toString());
        setjComboBoxCategoria(jTableProductos.getValueAt(this.jTableProductos.getSelectedRow(),3).toString());
    }//GEN-LAST:event_jTableProductosMouseClicked

    /**
     * Evento Boton limpiar
     * @param evt 
     */
    private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed
        Limpiar();
        setjTextFieldBuscar("");
        jTextFieldBuscarKeyReleased(null);
        jTableProductos.setModel(this.getCustomTableModel());
    }//GEN-LAST:event_jButtonLimpiarActionPerformed

    /**
     * Evento boton guardar, realiza update o inserter segun validacion de campo ID 
     * @param evt 
     */
    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        this.bodegaController = new Controlador.Bodega();
        if (getjTextFieldID().isEmpty()) {
            bodegaController.ingresa(this.getjTextFieldNombre(),this.getjComboBoxPlataformaINDEX(),
            this.getjComboBoxCategoriaINDEX(), this.getjTextFieldCantidad(), this.getjTextFieldPrecio());
            Limpiar();
            setjTextFieldBuscar("");
            jTextFieldBuscarKeyReleased(null);
            jTableProductos.setModel(this.getCustomTableModel());
        }else{
            bodegaController.actualiza(this.getjTextFieldID(),this.getjTextFieldNombre(),this.getjComboBoxPlataformaINDEX(),
                    this.getjComboBoxCategoriaINDEX(), this.getjTextFieldCantidad(), this.getjTextFieldPrecio());
            Limpiar();
            setjTextFieldBuscar("");
            jTextFieldBuscarKeyReleased(null);
            jTableProductos.setModel(this.getCustomTableModel()); 
        }
    }//GEN-LAST:event_jButtonGuardarActionPerformed
    /**
     * Evento jtexfield buscar
     * @param evt 
     */
    private void jTextFieldBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldBuscarKeyReleased
        DefaultTableModel modeloStock = (DefaultTableModel) this.jTableProductos.getModel();
        trsfiltro = new TableRowSorter(modeloStock);
        this.jTableProductos.setRowSorter(trsfiltro);
        // (?!) case insensitive al campo buscar
        trsfiltro.setRowFilter(RowFilter.regexFilter("(?i)"+jTextFieldBuscar.getText(), 1));
        Limpiar();
    }//GEN-LAST:event_jTextFieldBuscarKeyReleased
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex){
            java.util.logging.Logger.getLogger(Bodega.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex){
            java.util.logging.Logger.getLogger(Bodega.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex){
            java.util.logging.Logger.getLogger(Bodega.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex){
            java.util.logging.Logger.getLogger(Bodega.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
                new Bodega().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonIngresa;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JComboBox jComboBoxCategoria;
    private javax.swing.JComboBox jComboBoxPlataforma;
    private javax.swing.JLabel jLabelBuscar;
    private javax.swing.JLabel jLabelCantidad;
    private javax.swing.JLabel jLabelCategoria;
    private javax.swing.JLabel jLabelID;
    private javax.swing.JLabel jLabelNombre;
    private javax.swing.JLabel jLabelPlataforma;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelProducto;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableProductos;
    private javax.swing.JTextField jTextFieldBuscar;
    private javax.swing.JTextField jTextFieldCantidad;
    private javax.swing.JTextField jTextFieldID;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldPrecio;
    // End of variables declaration//GEN-END:variables

    /**
     * Metodo para limpiar formulario
     */
    public void Limpiar(){
        setjTextFieldID("");
        setjTextFieldNombre("");
        setjComboBoxCategoria("");
        setjComboBoxPlataforma("");
        setjTextFieldPrecio("");
        setjTextFieldCantidad("");
    }
    
    /** 
    * Metodo para llenado de jtable inventario
    * @return 
    */
    private TableModel getCustomTableModel(){
        this.bodegaController = new Controlador.Bodega();
        return new javax.swing.table.DefaultTableModel(
                this.bodegaController.getContenidoJTable(),
                this.bodegaController.getCabeceraJTable()
            ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class,java.lang.String.class,java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };
          
    }

}
//END